package com.example.databindingtest.fragment

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.databindingtest.R
import com.example.databindingtest.adapter.OnSellListAdapter
import com.example.databindingtest.base.BaseVmFragment
import com.example.databindingtest.databinding.FragmentOnSellBinding
import com.example.databindingtest.viewModel.OnSellViewModel
import kotlinx.android.synthetic.main.fragment_on_sell.view.*

class OnSellFragment : BaseVmFragment<FragmentOnSellBinding, OnSellViewModel>() {

    override fun getSubVmClass(): Class<OnSellViewModel> = OnSellViewModel::class.java

    override fun getSubLayoutId() = R.layout.fragment_on_sell

    override fun startLoadData() {
        //加载数据
        viewModel.loadData()
    }

    override fun observerData() {
        //观察ViewModel里的数据变化
        viewModel.loadState.observe(this, Observer {
            println(it.toString())
            //更新ui
        })

        viewModel.contentList.observe(this, Observer {
            //当内容列表发生变化的时候，就会通知到这里
            //更新列表
            mAdapter.setData(it)
        })
    }

    private val mAdapter by lazy {
        OnSellListAdapter()
    }

    override fun initView() {
        rootView.onSellListView.run {
            //布局管理器
            layoutManager = LinearLayoutManager(context)
            //设置适配器
            adapter = mAdapter
            //设置间距

        }
    }

}